package com.cb.controller.admin.order;


import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.controller.BaseFormController;
import com.cb.domain.model.order.Policy;
import com.cb.domain.service.excelWork.POLICY_TYPE;
import com.cb.domain.service.order.PolicyManager;
import com.cb.domain.service.system.ExlDataManager;

/**
 * 
 * @Author author name
 * Create Time:
 * <p>Class Description Here [TODO]</p>
 */
public class PolicyFormController extends BaseFormController {
	
	@Autowired
	private PolicyManager policyManager=null;
	@Autowired
	private ExlDataManager exlDataManager=null;
	

	protected Map<String,String> type;
	
	public void setType(Map<String, String> type) {
		this.type = type;
	}

	public PolicyFormController() {
		 setCommandName("policy");
	     setCommandClass(Policy.class);
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Policy policy=(Policy)command;
		
		
		String [] day=request.getParameterValues("daycondition");
		String selectDay="";
		if(null!=day){
			for (String string : day) {
				selectDay+=string;
			}
		}
		policy.setDaycondition(selectDay);
		String msg=null;
		policy.setFlightCode(policy.getFlightCode().toUpperCase());
		policy.setCabin(policy.getCabin().toUpperCase());
		
		String flightCondition="";
		if(policy.getFlightNumLimit().equalsIgnoreCase("2")){
			flightCondition=request.getParameter("withOut");
		}else if(policy.getFlightNumLimit().equalsIgnoreCase("3")){
				flightCondition=request.getParameter("withIn");
		}
		policy.setFlightCondition(flightCondition);
		
		//validate the logic business data here [TODO]
		if(policy.getPolicyId()==null){
			msg=getText("policy.added",policy.getPolicyId());
		}else{
			msg=getText("policy.updated",policy.getPolicyId());
		}
		String policyType=request.getParameter("policyType");
		policy.setCreateTime(new Date());
/*		String cabinNote=request.getParameter("cabinNote");

		String backNote=request.getParameter("backNote");
		policy.setCabin(cabinNote);
		policy.setBackNote(backNote);*/
		if(StringUtils.isEmpty(policyType)){
			msg="政策缺少类型";
			return new ModelAndView(getSuccessView());
		}else{
			policy.setPolicyType(Long.valueOf(policyType));
			policyManager.save(policy);
		}
		//String ptype=request.getParameter("ptype");
		//String postStr=exlDataManager.doFormWorkByType(getPolicyType(ptype), policy);
		//logger.info(postStr);
		saveMessage(request,msg);
		
		return new ModelAndView(getSuccessView());
	}
	
	private POLICY_TYPE getPolicyType(String type){
		if(type.equals(POLICY_TYPE.APPLY.toString())){
			return POLICY_TYPE.APPLY;
		}else if(type.equals(POLICY_TYPE.COMMON.toString())){
			return POLICY_TYPE.COMMON;
		}else if(type.equals(POLICY_TYPE.LOWPRICE.toString())){
			return POLICY_TYPE.LOWPRICE;
		}else if(type.equals(POLICY_TYPE.PREPAY.toString())){
			return POLICY_TYPE.PREPAY;
		}else{
			return POLICY_TYPE.CUSTOMER;
		}
	} 
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Policy policy=null;
		String policyId=request.getParameter("policyId");
		if(StringUtils.isNotEmpty(policyId)){
			policy=policyManager.get(new Long(policyId));
		}else{
			policy=new Policy();
			policy.setStatus(1l);
		}
		String selectDay=policy.getDaycondition();
		char [] selectDayArry=null;
		if(StringUtils.isNotBlank(selectDay)){
			selectDayArry=selectDay.toCharArray();
		}
		request.setAttribute("selectDayArry", selectDayArry);
		
		String [] dayArry=new String[]{"1","2","3","4","5","6","7"};
		request.setAttribute("dayArry", dayArry);
		String typeKey=request.getParameter("type");
		String formView=type.get(typeKey);
		setFormView(formView);
		return policy;
	}
	
}
